package util2;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierUtil {
	public static void main(String[] args) {
		CyclicBarrier cyclicBarrier = new CyclicBarrier(10,new Runnable() {
			
			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName() + "hahahah 搞完了。。。。");
				
			}
		});
		for (int i = 0; i < 10; i++) {
			if(i == 5) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("重置");
				cyclicBarrier.reset();
			}
			new Thread(()->{
				try {
					Thread.sleep(new Random().nextInt(1000));
					System.out.println(Thread.currentThread().getName()+":来了");
					
					cyclicBarrier.await();
					System.out.println("都来齐了，准备干活...;" +Thread.currentThread().getName()+":done");
				} catch (InterruptedException | BrokenBarrierException e) {
					e.printStackTrace();
				}
			}).start();
		}
		
	}
}
